
const { app, BrowserWindow, dialog, ipcMain } = require('electron')
const path = require('path')

app.whenReady().then(()=>{
  createWindow()
})

// 创建窗口
let win = null
function createWindow() {
  const width = 300
  const height = 430
  win = new BrowserWindow({
    width: width,
    height: height,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      sandbox: false,
    },
  })
  win.loadFile(path.join(__dirname,'../renderer/index.html'))
  win.webContents.openDevTools({mode: 'detach'})

  ipcMain.on('open-dialog',(event,type)=>{
    console.log(type,'type')
    if(type==='open') {
      showOpenDialog()
    }else if(type==='save') {
      showSaveDialog()
    }else if(type==='message') {
      showMessageBox()
    }
  })
  ipcMain.on('save-text',(event,data)=>{
    console.log(data,'data')
    showSaveDialog()
  })
  
}

// 选择对话框
function showOpenDialog() {
  dialog.showOpenDialog({
    properties: ['openFile','multiSelections','openDirectory','showHiddenFiles'],
    filters: [
      { name: '图片', extensions: ['jpg', 'png', 'gif'] },
      { name: '视频', extensions: ['mkv', 'avi', 'mp4'] },
      { name: '自定义文件类型', extensions: ['json'] },
      { name: '任意类型', extensions: ['*'] },
    ],
  }).then(err=>console.log(err))
}

// 保存对话框
function showSaveDialog() {
  dialog.showSaveDialog({
    title: '保存到某个文件'
  }).then(err=>console.log(err))
}

// 消息对话框
function showMessageBox() {
  dialog.showMessageBox(win,{
    icon: path.join(process.cwd(), 'doc/logoTemplate.png'),
    type: 'info',
    title: '消息标题',
    message: '消息内容',
    detail: '更详细的信息',
    buttons: ['按钮1', '按钮2'],
    defaultId: 1,
  }).then(err=>console.log(err))
}
